/* This file is part of FireEscape.
 *
 * FireEscape is the intellectual work of Phillip Cheng & Allen Park.
 * All future releases or modifications of this program must include this
 * attribuation as according to the Creative Commons BY-SA license.
 *
 * FireEscape is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * FireEscape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FireEscape.  If not, see <http://www.gnu.org/licenses/>.
 *
 * All modifications to this program should include this original statement.
 *
 * If you modify, distribute, or use the program , please contact
 * fireescape@kloudvine.com
 * http://code.google.com/p/fireescape/
 *
 * If you are interested in using FireEscape for your structure, please contact us
 * for support.
 *
 * If you do use FireEscape, please consider donating to the project.
 */
package fireescape;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 * Converts from routes to image files
 * @author HP_Administrator
 */
public class RouteToImage extends Applet {

    public static String routeDirectory;
    private ArrayList<Route> floorRoutes;
    //private ArrayList<Color> colors = new ArrayList<Color>();
    Genome genome = null;

    /**
     * Initialization method that will be called after the applet is loaded
     * into the browser.
     */
    @Override
    public void init() {
    }

    /**
     * Instantiates the route to image converter
     * @param g
     */
    public RouteToImage(Genome g) {
        super();
        this.genome = g;
        this.floorRoutes = (ArrayList<Route>) g.getList();
        /*colors.add(Color.CYAN);
        colors.add(Color.DARK_GRAY);
        colors.add(Color.GREEN);
        colors.add(Color.orange);
        colors.add(Color.magenta);
        colors.add(Color.RED);
        colors.add(Color.yellow);
        colors.add(Color.pink);
        colors.add(Color.BLUE);
        colors.add(Color.lightGray);
        colors.add(Color.black);*/
    }

    /**
     * Prints the route to a folder
     */
    public void printRoute() {
        Image[] imgs = routeToImage();
        for (int k = 0; k < Map.getNumFloors(); k++) {
            try {
                File h = new File(routeDirectory + genome.getHash() + "-" + k + ".png");
                h.createNewFile();
                ImageIO.write((RenderedImage) imgs[k], "png", h);
            } catch (IOException ex) {
                Logger.getLogger("FireEscapeLog").log(Level.SEVERE,null,ex);
            }
        }
        this.stop();
        this.destroy();
    }

    /**
     * Processes a genome into images of the floors with the routes
     * @return An array of the images
     */
    public Image[] routeToImage() {
        Image[] floorImages = new Image[Map.getNumFloors()];
        Graphics2D[] floorGraphics = new Graphics2D[Map.getNumFloors()];
        for (int i = 0; i < Map.getNumFloors(); i++) {
            floorImages[i] = createImage((int) (FireEscapeGUI.xSize * FireEscapeGUI.scale), (int) (FireEscapeGUI.ySize * FireEscapeGUI.scale));
            floorGraphics[i] = (Graphics2D) floorImages[i].getGraphics();
            floorGraphics[i].setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            Map.getFloor(i).paintRoomsOnly(floorGraphics[i]);//copyFloorImage();
        }
        //int k = -1;
        for (int j = 0; j < floorRoutes.size(); j++) {
            //k++;
            //k %= colors.size();
            Route route = floorRoutes.get(j);
            Room firstRoom = Map.getRoom(route.getFirstRoom());
            Graphics2D g = floorGraphics[firstRoom.getFloorNum()];
            Color randomColor = new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
            //g.setColor(colors.get(k));
            g.setColor(randomColor);
            //g.fill(new Ellipse2D.Double(FireEscapeGUI.scale * (firstRoom.getCenterX() - 5), FireEscapeGUI.scale * (firstRoom.getCenterY() - 5), FireEscapeGUI.xSize * 10, FireEscapeGUI.ySize * 10));
            g.fill(new Ellipse2D.Double(FireEscapeGUI.scale * (firstRoom.getCenterX() - 5), FireEscapeGUI.scale * (firstRoom.getCenterY() - 5), FireEscapeGUI.scale * 10, FireEscapeGUI.scale * 10));
            Point2D.Double pastPoint = new Point2D.Double(FireEscapeGUI.scale * firstRoom.getCenterX(), FireEscapeGUI.scale * firstRoom.getCenterY());
            for (int i = 0; i < route.getRouteSize(); i++) {
                Exit exit = route.getNavPoint(i);
                Point2D.Double currentPoint = new Point2D.Double(FireEscapeGUI.scale * exit.getRandomX(), FireEscapeGUI.scale * exit.getRandomY());
                g.draw(new Line2D.Double(pastPoint, currentPoint));
                if (exit instanceof Staircase) {
                    g = floorGraphics[Map.getRoom(exit.connectedToID(-1)).getFloorNum()];
                    g.setColor(randomColor);
                }
                pastPoint = currentPoint;
            }
        }
        return floorImages;
    }
    // TODO overwrite start(), stop() and destroy() methods
}
